package com.imesne.office.excel.model;

import com.imesne.office.excel.exception.OfficeException;
import com.imesne.office.excel.utils.ExcelKitUtils;
import org.apache.poi.ss.usermodel.Row;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by liyd on 17/7/3.
 */
public class ExcelRow extends ExcelProperties {

    private Row row;

    /**
     * 行高
     */
    private Integer height;

    /**
     * 行号
     */
    private Integer rowNum;


    private Map<Integer, ExcelCell> excelCells;

    public ExcelRow() {
        excelCells = new LinkedHashMap<>();
    }

    public void addCell(ExcelCell excelCell) {
        if (excelCell.getCellNum() == null) {
            throw new OfficeException("列索引号不能为空");
        }
        excelCells.put(excelCell.getCellNum(), excelCell);
    }

    public ExcelCell createCell(int cellNum) {
        return this.createCell(cellNum, null);
    }

    public ExcelCell createCell(int cellNum, Object value) {
        ExcelCell excelCell = new ExcelCell();
        excelCell.setCellNum(cellNum);
        excelCell.setValue(value);
        this.addCell(excelCell);
        return excelCell;
    }

    public ExcelCell createCell(String y) {
        return this.createCell(y, null);
    }

    public ExcelCell createCell(String y, Object value) {
        int num = ExcelKitUtils.columnToNum(y);
        return this.createCell(num, value);
    }

    public Collection<ExcelCell> getExcelCells() {
        return excelCells.values();
    }

    public ExcelCell getCell(int cellNum) {
        return this.excelCells.get(cellNum);
    }

    public ExcelCell getCell(String y) {
        int num = ExcelKitUtils.columnToNum(y);
        return this.getCell(num);
    }

    public Object getCellValue(int cellNum) {
        ExcelCell excelCell = this.excelCells.get(cellNum);
        return excelCell == null ? null : excelCell.getValue();
    }

    public Object getCellValue(String y) {
        int num = ExcelKitUtils.columnToNum(y);
        return this.getCellValue(num);
    }

    public String getCellStringValue(int cellNum) {
        ExcelCell excelCell = this.excelCells.get(cellNum);
        return excelCell == null ? null : String.valueOf(excelCell.getValue());
    }

    public String getCellStringValue(String y) {
        int num = ExcelKitUtils.columnToNum(y);
        return this.getCellStringValue(num);
    }

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();
        sb.append(rowNum).append(" > ");
        for (Map.Entry<Integer, ExcelCell> entry : excelCells.entrySet()) {
            sb.append("[").append(ExcelKitUtils.numToColumn(entry.getKey()) + ":" + entry.getValue()).append("] ");
        }
        return sb.toString();
    }

    public Row getRow() {
        return row;
    }

    public void setRow(Row row) {
        this.row = row;
    }

    public Integer getRowNum() {
        return rowNum;
    }

    public void setRowNum(Integer rowNum) {
        this.rowNum = rowNum;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }
}
